Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Kafka SASL OAUTH token refreshing #2834

Merged
merged 13 commits into from
Sep 26, 2024
Merged

feat: Kafka SASL OAUTH token refreshing #2834

merged 13 commits into from
Sep 26, 2024

Conversation

vlastahajek
Copy link
Contributor

@vlastahajek vlastahajek commented Sep 18, 2024

Required checklist

  • Sample config files updated (both /etc folder and NewDemoConfig methods) (influxdb and plutonium)
  • openapi swagger.yml updated (if modified API) - link openapi PR
  • Signed CLA (if not already signed)

Description

Adding SASL OAUTH token refreshing for Kafka plugin. The same support as Telegraf has - custom, Auth0, AzureAD.

Kapacitor docs update PR: #5617

New Kafka config params:

  • sasl-oauth-service
  • sasl-oauth-client-id
  • sasl-oauth-client-secret
  • sasl-oauth-token-url
  • sasl-oauth-token-expiry-margin
  • [kafka.sasl-oauth-parameters]
  • sasl-oauth-tenant-id
  • sasl-oauth-scopes

@vlastahajek vlastahajek marked this pull request as draft September 18, 2024 11:39
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlastahajek nice work! I know you plan to clean this up a bit more but here are some early comments nonetheless. ;-)

.circleci/config.yml Outdated Show resolved Hide resolved
Comment on lines 596 to 602
# sasl-oauth-service = ""
## The client ID to use when authenticating with SASL/OAUTH.
# sasl-oauth-client-id = ""
## The client secret to use when authenticating with SASL/OAUTH.
# sasl-oauth-client-secret = ""
## The token URL to use when authenticating with SASL/OAUTH.
# sasl-oauth-token-url = ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sasl-oauth-service and sasl-oauth-token-url are somewhat redundant aren't they? Do we really want both or should we just use the URL and provide the URLs for common services in the config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, the core implementation and feature set are done according to Telegraf to provide a similar user experience
Choosing service makes sense to distinguish auth0, azure and other and provides space for future extensions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know as I implemented this part in Telegraf. The big issue is that the two are redundant and you need to decide and document the behavior if both options are given and, in the worst case, contradict each other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only contradiction I see, for now, is we don't need the token-url for azure.

I still don't understand how it would work if there were only one. The token-url?

  • we need to check if the audience parameter is added for auth0
  • If a user would like to use azuread, the token-url would be azuread?
  • In case of azure we need to check tenant-id

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying you have to remove it but then please make the use and redundancy clear in the option description! As it is now, people might be inclined to specify both the token-url and the service.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, the token-url param should be more contextually described

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"

kafka "github.com/IBM/sarama"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the alias and just use

Suggested change
kafka "github.com/IBM/sarama"
"github.com/IBM/sarama"

as everything else is confusing...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very subjective. As the sarama is a Kafka client the alias makes it more clean. Also, the alias is already used in kafka/service.go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I don't like this but it might be OK if kapacitor guys agree. Let me give you my reasons:

  1. You obfuscate the library used with an alias, making it harder for readers of the code to figure out what's going on.
  2. Your chosen alias is the same as the package name, this will cause additional confusion.
  3. You increase the PR size without any necessity.

But as I said, if that's how it is done in kapacitor I don't mind.

Comment on lines 47 to 49
if k.SASLMechanism == "" {
return nil
}
switch k.SASLMechanism {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if k.SASLMechanism == "" {
return nil
}
switch k.SASLMechanism {
switch k.SASLMechanism {
case "":
return nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

services/kafka/sasl.go Outdated Show resolved Hide resolved
Comment on lines 129 to 128
if k.SASLOAUTHExpiryMargin == 0 {
k.SASLOAUTHExpiryMargin = 10 * time.Second
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was initialized before in the config, wasn't it? The current way the user cannot set the option to a valid zero...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Comment on lines 209 to 218
func azureAD(tenant string) oauth2.Endpoint {
if tenant == "" {
tenant = "common"
}
return oauth2.Endpoint{
AuthURL: "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/authorize",
TokenURL: "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/token",
DeviceAuthURL: "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/devicecode",
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this hand-crafted implementation, why not rely on https://pkg.go.dev/golang.org/x/oauth2@v0.23.0/endpoints?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was mistakenly copied.

Comment on lines +21 to +27
func NewRefreshingToken(source oauth2.TokenSource, cancel context.CancelFunc, extensions map[string]string) *RefreshingToken {
return &RefreshingToken{
source: source,
cancel: cancel,
extensions: extensions,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cancel function is never used in here and actually shouldn't be. The caller should cancel the context on exit...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, handling closing is still TODO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@vlastahajek vlastahajek changed the title Feat/kafka oauth feat: Kafka SASL OAUTH token refreshing Sep 24, 2024
@vlastahajek vlastahajek marked this pull request as ready for review September 24, 2024 14:39
Copy link
Contributor

@bednar bednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlastahajek, thanks for the nice improvement 👍. I've just added some minor requirements before acceptance.

# sasl-oauth-client-secret = ""
## The token URL to use when sasl-oauth-service is custom or auth0. Leave empty otherwise.
# sasl-oauth-token-url = ""
## The expiry margin for the token.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small suggestion to clarify the meaning of this property:

Suggested change
## The expiry margin for the token.
## The margin for the token's expiration time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

@@ -49,7 +49,7 @@ type Config struct {
}

func NewConfig() Config {
return Config{ID: DefaultID}
return Config{ID: DefaultID, SASLAuth: SASLAuth{SASLOAUTHExpiryMargin: 10 * time.Second}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define the SASLOAUTHExpiryMargin value as a constant, similar to DefaultID.

Suggested change
return Config{ID: DefaultID, SASLAuth: SASLAuth{SASLOAUTHExpiryMargin: 10 * time.Second}}
return Config{ID: DefaultID, SASLAuth: SASLAuth{SASLOAUTHExpiryMargin: DefaultSASLOAUTHExpiryMargin}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

@vlastahajek vlastahajek requested a review from bednar September 25, 2024 14:32
Copy link
Contributor

@bednar bednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vlastahajek thanks for PR 👍

LGTM 🚀

@bednar bednar merged commit 71a5e1e into master Sep 26, 2024
3 checks passed
@bednar bednar deleted the feat/kafka-oauth branch September 26, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants